import os
import openpyxl
import xlwt

HANDLE_PATH = '.'
files = [f for f in os.listdir(HANDLE_PATH) if os.path.isfile(f) and f.endswith('.xlsx')]
paths = "./转换之后/"
# 检查路径是否存在，如果不存在则创建
if not os.path.exists(paths):
    os.makedirs(paths)

for file_path in files:
    new_file_path = os.path.join(paths, file_path)
    # 检查文件是否已经被处理过
    if os.path.exists(new_file_path):
        continue  # 如果文件已经被处理过，跳过该文件

    workbook = openpyxl.load_workbook(file_path)

    # 获取最后一个sheet
    last_sheet_name = workbook.sheetnames[-1]
    last_sheet = workbook[last_sheet_name]

    data = []
    purchase_order_number = None

    for row in last_sheet.iter_rows():
        row_values = [cell.value for cell in row]
        if row_values[0] == "采购单号：":
            purchase_order_number = row_values[1]
        elif isinstance(row_values[0], int):  # 序号列
            data.append([
                row_values[1],  # 零件号
                row_values[2],  # 零件名称(中文）
                row_values[4],  # 单位
                row_values[5]   # 送货数量
            ])

    new_workbook = xlwt.Workbook()
    new_worksheet = new_workbook.add_sheet('供应商供货清单')

    info = [
        ('发运编号：', '', '', '', '', '', '客户：', '成都延锋安道拓汽车部件系统有限公司', '', '', ''),
        ('供应商', '', '', '', '', '', '采购订单编号：', '', '', '', ''),
        ('供应商地址：', '', '', '', '', '', '收货地址：', '', '', '', ''),
        ('序号', '零件号', '零件名称', '到货数量', '单位', '使用车型', '到货包装数', '标准包装数', '批次', '送货通知单号', '备注')
    ]

    font_style = xlwt.easyxf('font: height 280, bold on;')  # 设置字体大小为14，加粗
    borders = xlwt.Borders()  # 创建边框对象
    borders.left = xlwt.Borders.THIN
    borders.right = xlwt.Borders.THIN
    borders.top = xlwt.Borders.THIN
    borders.bottom = xlwt.Borders.THIN
    font_style.borders = borders  # 将边框应用到字体样式

    for row, row_data in enumerate(info):
        for col, cell_data in enumerate(row_data):
            new_worksheet.write(row + 1, col, cell_data, font_style)

    for col in range(11):  # 设置所有列的宽度为15个字符宽度
        new_worksheet.col(col).width = 256 * 15

    title_style = xlwt.easyxf('align: horiz center, vert center; font: height 280, bold on;')
    title_style.borders = borders
    new_worksheet.write_merge(0, 0, 0, 10, '供应商供货清单（国内供应商）', title_style)

    data_style = xlwt.easyxf('font: height 280;')
    data_style.borders = borders

    for index, (part_number, part_name, unit, arrival_quantity) in enumerate(data):
        new_worksheet.write(index + 5, 0, index + 1, data_style)
        new_worksheet.write(index + 5, 1, part_number, data_style)
        new_worksheet.write(index + 5, 2, part_name, data_style)
        new_worksheet.write(index + 5, 3, arrival_quantity, data_style)
        new_worksheet.write(index + 5, 4, unit, data_style)  # 单位
        new_worksheet.write(index + 5, 5, '', data_style)  # 使用车型
        new_worksheet.write(index + 5, 6, '', data_style)  # 到货包装数
        new_worksheet.write(index + 5, 7, '', data_style)  # 标准包装数
        new_worksheet.write(index + 5, 8, '', data_style)  # 批次
        new_worksheet.write(index + 5, 9, purchase_order_number, data_style)
        new_worksheet.write(index + 5, 10, '', data_style)  # 备注

    new_file_path = os.path.join(paths, os.path.splitext(file_path)[0] + '.xls')
    new_workbook.save(new_file_path)
